package com.yzt.zhuju.widget.widget;

import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.opensource.svgaplayer.SVGAImageView;

/**
 * User: fish
 * E-mail: 709913163@qq.com
 * Date: 2023/1/10
 * Time: 16:15
 * describe:
 */
public class SVGALoadPlayer {

    public static void loadSvga(SVGAImageView mSVGAImageView, String url){
        loadSvga(mSVGAImageView, url, null);
    }
    public static void loadSvga(SVGAImageView mSVGAImageView, String url, OnLoadSvgaListener onLoadSvgaListener){
        Glide.with(mSVGAImageView.getContext())
                .load(url)
                .addListener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (onLoadSvgaListener != null) {
                            onLoadSvgaListener.onLoadEnd();
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (onLoadSvgaListener != null) {
                            onLoadSvgaListener.onLoadSuccess();
                        }
                        return false;
                    }
                })
                .into(mSVGAImageView);
    }

    public interface OnLoadSvgaListener{
        void onLoadSuccess();
        void onLoadEnd();
    }
}
